#include <errno.h>

#include <linux/sched.h>

long last_pid = 0;

int copy_process(int nr)
{
    struct task_struct *p;

    p = (struct task_struct *) get_free_page();
    if (!p)
        return -EAGAIN;
    task[nr] = p;

    printk("task struct address: 0x%x\n\r", p);

    return last_pid;
}

int find_empty_process(void)
{
    int i;

repeat:    // 获取不重复的pid
    if ((++last_pid) < 0)
        last_pid = 1;
    for(i = 0; i < NR_TASKS; i++)
        if (task[i] && task[i]->pid == last_pid)
            goto repeat;
    for(i = 1; i < NR_TASKS; i++)    // 把任务0排除在外
        if (!task[i])
            return i;
    return -EAGAIN;
}